home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / ch10 / CountWordsCmd.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.7 KB  |  84 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. //////////////////////////////////////////////////////////////////
  22. // CountWordsCmd.h: Count the frequency of words in a file
  23. /////////////////////////////////////////////////////////////////
  24. #ifndef COUNTWORDSCMD_H
  25. #define COUNTWORDSCMD_H
  26. #include <string.h>   // Needed for strcmp
  27. #include <stdio.h>   // Needed for strcmp
  28. #include "InterruptibleCmd.h"
  29.  
  30. // The Word class stores a string and maintains a count field
  31. // used to record how many times the word has been encountered
  32.  
  33. class Word {
  34.     
  35.   private:
  36.     
  37.     char *_word;   // The "word" represented by this object
  38.     int   _count;  // Number of times this word has been found
  39.     
  40.   public:
  41.     
  42.     Word ( char *str ) { _word = strdup ( str ); _count = 1; }
  43.     ~Word() { delete _word; }
  44.     
  45.     // Compare the character string stored in this object
  46.     // to another character string
  47.     
  48.     int operator== ( char *str ) { return !strcmp ( _word, str ); }
  49.     
  50.     // Increment the count associated with this word
  51.     
  52.     void operator++() { _count++ ; } 
  53.     char *word()  { return _word; }
  54.     int   count() { return _count; }
  55. };
  56.  
  57.  
  58. class CountWordsCmd : public InterruptibleCmd {
  59.     
  60.   private:
  61.     
  62.     Word  **_list;        // List of words found in the file
  63.     int   _numWords;  // Size of the _list
  64.     long  _fileSize;  // Total size of the file in bytes
  65.     int   _bytesRead; // How much of the file has been processed
  66.     FILE *_fd;        // The file being read
  67.     int   _percentDone; 
  68.     
  69.     void saveWord ( char * );  // Add a word to the _list
  70.     
  71.   protected:
  72.     
  73.     void doit();      // The function that performs the work
  74.     
  75.   public:
  76.     
  77.     CountWordsCmd ( char *, int , char * );
  78.     virtual ~CountWordsCmd ();
  79.     int   numWords () { return _numWords; }
  80.     char *getWord ( int i ) { return _list[i]->word(); }
  81.     int   getCount ( int i ) { return _list[i]->count(); }
  82. };
  83. #endif
  84.